package com.hdjk.view.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/2.
 */

public class MapJsonUtil {

    public static void JsonToHashMap(String jsonString, Map<String, Object> rstList) {
        try {
            JSONObject jsonObjResult=new JSONObject(jsonString);
            JsonToHashMap(jsonObjResult, rstList);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void JsonToHashMap(JSONObject jsonData, Map<String, Object> rstList) {
        try {
            for (Iterator<String> keyStr = jsonData.keys(); keyStr.hasNext();) {

                String key1 = keyStr.next().trim();
                if (jsonData.get(key1) instanceof JSONObject) {
                    HashMap<String, Object> mapObj = new HashMap<String, Object>();
                    JsonToHashMap((JSONObject) jsonData.get(key1), mapObj);
                    rstList.put(key1, mapObj);
                    continue;
                }
                if (jsonData.get(key1) instanceof JSONArray) {
                    ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();

                    JsonToHashMap((JSONArray) jsonData.get(key1), arrayList);
                    rstList.put(key1, arrayList);
                    continue;
                }
                JsonToHashMap(key1, jsonData.get(key1), rstList);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private static void JsonToHashMap(JSONArray jsonarray, List<Map<String, Object>> rstList) {
        try {
            for (int i = 0; i < jsonarray.length(); i++) {
                if (jsonarray.get(i) instanceof JSONObject) {

                    HashMap<String, Object> mapObj = new HashMap<String, Object>();
                    JsonToHashMap((JSONObject) jsonarray.get(i), mapObj);
                    rstList.add(mapObj);
                    continue;
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private static void JsonToHashMap(String key, Object value,
                                      Map<String, Object> rstList) {
        if (value instanceof String) {
            rstList.put(key, (String) value);
            return;
        }
        rstList.put(key, value);
    }

    public static void HashMapToJson(Map<?,?> copyFrom, JSONObject jsonObj) {
        try {
            Map<?,?> contentsTyped = (Map<?,?>) copyFrom;
            for (Map.Entry<?, ?> entry : contentsTyped.entrySet()) {
                String key = (String) entry.getKey();
                jsonObj.put(key, wrap(entry.getValue()));
            }
        } catch (Exception e) {
        }
    }

    private static Object wrap(Object o) {
        if (o == null) {
            return null;
        }
        if (o instanceof JSONArray || o instanceof JSONObject) {
            return o;
        }
        if (o.equals(null)) {
            return o;
        }
        try {
            if (o instanceof Collection) {
                return new JSONArray((Collection<?>) o);
            }
            if (o instanceof Map) {
                JSONObject jsonObj = new JSONObject();
                HashMapToJson((Map<?,?>) o, jsonObj);
                return jsonObj;
            }
            if (o instanceof Boolean ||
                    o instanceof Byte ||
                    o instanceof Character ||
                    o instanceof Double ||
                    o instanceof Float ||
                    o instanceof Integer ||
                    o instanceof Long ||
                    o instanceof Short ||
                    o instanceof String) {
                return o;
            }
            if (o.getClass().getPackage().getName().startsWith("java.")) {
                return o.toString();
            }
        } catch (Exception ignored) {
        }
        return null;
    }
}
